package com.xiao.controller;

import com.xiao.pojo.User;
import com.xiao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public void  login(@RequestParam("username") String username, @RequestParam("password") String password, HttpServletRequest request, HttpServletResponse response, Model model) throws ServletException, IOException {
        User user = userService.selectUserByUserName(username);

        HttpSession session = request.getSession();
        if (user != null) {
            if (user.getPassword().equals(password)) {
                System.out.println("登录成功");
                session.setAttribute("user",user);
                response.sendRedirect(request.getContextPath()+"/jsp/admin_page.jsp");




            } else {
                System.out.println("账号或密码错误");
                request.setAttribute("login_msg","用户名或密码错误");
                request.getRequestDispatcher("/jsp/admin-login.jsp").forward(request,response);
            }
        } else {

            request.setAttribute("login_msg","账号不存在");
            request.getRequestDispatcher("/jsp/admin-login.jsp").forward(request,response);
        }

    }

}